package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.VarietyMapper;
import com.fantastic.mapper.zjs.VarietyVarietyTypeVoMapper;
import com.fantastic.model.pojo.zjs.Variety;
import com.fantastic.model.vo.zjs.*;
import com.fantastic.service.zjs.VarietyService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/30 10:50
 * @description: 菜品信息service层接口实现类
 */
@Service
public class VarietyServiceImpl implements VarietyService {

    @Resource
    private VarietyMapper varietyMapper;

    @Resource
    private VarietyVarietyTypeVoMapper varietyVarietyTypeVoMapper;

    /**
     * 查询
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param variety
     * @param sortStatus 排序状态  1为升序 (从小到大) 0为降序(从大到小)
     * @return
     */
    @Override
    public List<Variety> listVariety(Variety variety, Integer sortStatus) {
        List<Variety> varieties = varietyMapper.listVariety(variety, sortStatus);
        //调用数据工具类判断集合是否为null
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), varieties);
        for (Variety list : varieties) {
            list.setVarietyPicture(FilesUtils.getSlashParse(list.getVarietyPicture()));
        }
        return varieties;
    }

    /**
     * 查询单个菜品信息
     *
     * @param varietyId
     * @return
     */
    @Override
    public Variety getVariety(Integer varietyId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), varietyId);
        Variety variety = varietyMapper.getVariety(varietyId);
        variety.setVarietyPicture(FilesUtils.getSlashParse(variety.getVarietyPicture()));
        return variety;
    }

    /**
     * 查询菜品表和类型表关联信息
     *
     * @param varietyTypeId
     * @return
     */
    @Override
    public List<VarietyVo> listVarietyVo(Integer varietyTypeId) {
        List<VarietyVo> varietyVos = varietyMapper.listVarietyVo(varietyTypeId);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), varietyVos);
        for (VarietyVo vo : varietyVos) {
            vo.setVarietyTypePicture(FilesUtils.getSlashParse(vo.getVarietyTypePicture()));
            List<Variety> variety = vo.getVariety();
            for (Variety var : variety) {
                var.setVarietyPicture(FilesUtils.getSlashParse(var.getVarietyPicture()));
            }
        }
        return varietyVos;
    }

    /**
     * 查询菜品类型、菜品、菜品规格类型
     *
     * @return
     */
    @Override
    public List<VarietyVarietyTypeVo> listVarietyVarietySpecType() {
        List<VarietyVarietyTypeVo> varietyVarietyTypeVos = varietyVarietyTypeVoMapper.listVarietyVarietySpecType();
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), varietyVarietyTypeVos);
        for (VarietyVarietyTypeVo v1 : varietyVarietyTypeVos) {
            v1.setVarietyTypePicture(FilesUtils.getSlashParse(v1.getVarietyTypePicture()));
            List<VarietySpecsVo> varietySpecsVos = v1.getVarietySpecsVos();
            for (VarietySpecsVo v2 : varietySpecsVos) {
                v2.setVarietyPicture(FilesUtils.getSlashParse(v2.getVarietyPicture()));
            }
        }
        return varietyVarietyTypeVos;
    }


    /**
     * 添加菜品
     *
     * @param variety
     */
    @Override
    public Integer insertVariety(MultipartFile varietyFile, Variety variety) {
        //判断对象是否为null
        IsEmptyUtils.objectIsNull(new InsertException(RespStatusInfo.INSERT_ERROR), variety);
        //判断菜名、菜描述是否有
        IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.INSERT_ERROR),
                variety.getVarietyName(), variety.getVarietyDepict());
        //判断上架状态、是否外送状态是否为null。
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.INSERT_ERROR), variety.getVarietyGoStatus(), variety.getVarietyPutStatus());
        //判断有没有选择类型
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.INSERT_ERROR), variety.getVarietyTypeId());


        //文件不为null 或者文件大小不为0（表示传了个空文件）
        if (varietyFile != null && varietyFile.getSize() != 0) {
            //判断文件类型是否支持        调用getSuffix()对文件名进行后缀截取。再传入supportType()中判断是否支持
            MultipartFileUtils.supportType(varietyFile.getOriginalFilename());
            //将文件写入到本地，返回文件相对路径
            String relativePath = MultipartFileUtils.writeFile(varietyFile, "菜品");
            //添加到variety对象中
            variety.setVarietyPicture(relativePath);
            //设置默认出售数量为0
            variety.setVarietySell(0);
        } else {
            throw new InsertException(RespStatusInfo.FILETYPE_NULL);
        }


        return varietyMapper.insertVariety(variety);
    }

    /**
     * 修改菜品信息
     *
     * @param variety
     */
    @Override
    public Integer updateVariety(MultipartFile varietyFile, Variety variety) {
        //判断id是否存在即可
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.UPDATE_ERROR), variety.getVarietyId());
        if (varietyFile != null && varietyFile.getSize() != 0) {
            //将文件写入到本地，返回文件相对路径
            String relativePath = MultipartFileUtils.writeFile(varietyFile, "菜品");
            variety.setVarietyPicture(relativePath);

            //查询这个id的菜品信息
            Variety getVariety = getVariety(variety.getVarietyId());
            //通过原来的菜品图片路径来进行原来的图片删除
            MultipartFileUtils.deleteFile(getVariety.getVarietyPicture());
        }
        return varietyMapper.updateVariety(variety);
    }


    /**
     * 删除菜品信息
     *
     * @param varietyId
     */
    @Override
    public Integer deleteVariety(Integer varietyId) {
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DATA_ERROR), varietyId);
        return varietyMapper.deleteVariety(varietyId);
    }

}
